/**
 * 
 */
package com.mvity.framework.common.log;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.mvity.framework.common.annotation.ServiceDesc;

/**
 * 日志拦截器
 * 
 * @author vity
 * @date 2012-7-30
 * @mail vity.1215@gmail.com
 */
@Aspect
@Component
public class ServiceLogAspect implements Serializable {

	private static final long serialVersionUID = -1612538113467228597L;

	static final Logger logger = LoggerFactory.getLogger(ServiceLogAspect.class);

	/**
	 * 在执行 Service 方法之前拦截
	 * 
	 * @param point
	 */
	public void beforeService(JoinPoint point) {
		System.out.println(point.getKind()); // method-execution
		System.out.println(point.getTarget()); // com.mvity.framework.system.service.impl.UserServiceImpl@ec3716
		System.out.println(point.getThis()); // com.mvity.framework.system.service.impl.UserServiceImpl@ec3716
		if (point.getArgs() != null && point.getArgs().length == 2) {
			System.out.println(point.getArgs()[0]); //
			System.out.println(point.getArgs()[1]); //
		}
		System.out.println(point.getSignature().getDeclaringTypeName()); // String
		                                                                 // com.mvity.framework.system.service.UserService.userLogin(String,String)
		System.out.println(point.getSignature().getModifiers()); // String
		                                                         // com.mvity.framework.system.service.UserService.userLogin(String,String)
		System.out.println(point.getSignature().getName()); // String
		                                                    // com.mvity.framework.system.service.UserService.userLogin(String,String)
		System.out.println(point.getSignature().getDeclaringType()); // String
		                                                             // com.mvity.framework.system.service.UserService.userLogin(String,String)
		System.out.println("beforeService");
		
		// 操作者 
	}

	/**
	 * 异常拦截
	 * 
	 * @param point
	 */
	@AfterThrowing(pointcut = "execution(* com.mvity.framework.*.service.*.*(..)) && @annotation(serviceDesc)", throwing = "ex")
	public void afterException(JoinPoint joinPoint, ServiceDesc serviceDesc, Exception ex) {
		System.out.println(joinPoint.getTarget().getClass().getName());
		System.out.println(joinPoint.getSignature().getName()); // String
		System.out.println(serviceDesc.value());
		System.out.println("----------------------");
		System.out.println("exception: " + joinPoint.getSignature().getName());
	}

	/**
	 * 方法拦截
	 * 
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around("execution(* com.mvity.framework.*.service.*.*(..)) && @annotation(serviceDesc)")
	public Object aroundService(ProceedingJoinPoint joinPoint, ServiceDesc serviceDesc) throws Throwable {
		System.out.println(joinPoint.getTarget().getClass().getName());
		System.out.println(joinPoint.getSignature().getName()); // String
		System.out.println(serviceDesc.value());
		System.out.println("----------------------");
		StopWatch stopWatch = new StopWatch();
		System.out.println("Around: " + joinPoint.getSignature().getName());
		System.out.println("Before");
		stopWatch.start();
		Object obj = joinPoint.proceed();
		stopWatch.stop();
		System.out.println("End . " + stopWatch.getTime() + "ms");
		return obj;
	}
}
